package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author Thibault Loup
	 */
	public class SliceBmp extends Sprite
	{
		
		// __________________________________________________________
		// -										 	  CONSTRUCTOR
		public function SliceBmp(pBmpd:BitmapData)
		{
			_src = pBmpd;
			
			_build();
		}
		
		
		// __________________________________________________________
		// -										  PUBLICS METHODS
		
		public function get progress() : Number
		{
			return _progress;
		}

		public function set progress(pValue : Number) : void
		{
			if( _progress == pValue) return;
			
			_progress = pValue;
			
			_update();
		}
		
		
		// __________________________________________________________
		// -										 PRIVATES METHODS
		private function _build() : void
		{
			addChild(_display = new Bitmap());
			addChild(_debugTest = new Shape());
			
			_rect = new Rectangle();
			_rect.x = -_shiftX;
			_rect.y = -_shiftY;
			_rect.width = _src.width + _shiftX*2;
			_rect.height = _src.height + _shiftY*2;
			_coeffDir = _src.height/_shiftX;
			
			
			_dest = new BitmapData(_rect.width, _rect.height, true, 0);
			
			_startNode = _createItems();
			
			graphics.beginFill(0x00FF00, .5);
			graphics.drawRect(_rect.x, _rect.y, _rect.width, _rect.height);
			graphics.endFill();
			
			_display.x = _rect.x;
			_display.y = _rect.y;
			
			_test();
//			_update();
		}

		private function _createItems() : Item
		{
			const ln:int = int((_src.width / _stripeWidth) + (_shiftX/_stripeWidth));
			
			var rect:Rectangle = new Rectangle(0,0, _stripeWidth+_shiftX, _src.height);
			const point:Point = new Point();
			var bmpd:BitmapData;
			
			const start:Item = new Item;
			var prev:Item = start;
			var item:Item;
			
			var i:int = ln;
			
			var c:Sprite, bmp:Bitmap, s:Shape;
			c = new Sprite();
			c.addChild(bmp = new Bitmap());
			c.addChild(s = _createMask());
			bmp.mask = s;
			
			var bmpd2:BitmapData;
			
			while(--i >= 0)
			{
				rect.x = -_shiftX + (i)*_stripeWidth;
				
				bmpd = new BitmapData(rect.width, rect.height, true, 0);
				bmpd2 = new BitmapData(rect.width, rect.height, true, 0);
				bmpd.copyPixels(_src, rect, point);
				
				bmp.bitmapData = bmpd;
				bmpd2.draw(c, null, null, null, null, true);
				bmpd.dispose();
				
				item = new Item();
				item.bmpd = bmpd2;
				item.position.x = rect.x + _shiftX;
				item.position.y = _shiftY;
				
				_debugTest.graphics.lineStyle(1, 0xFF0000);
				_debugTest.graphics.moveTo(rect.x+_shiftX, 0);
				_debugTest.graphics.lineTo(rect.x, rect.height);
				_debugTest.graphics.endFill();
				
				prev.next = item;
				prev = item;
			}

			return start;
		}
		
		private function _update():void
		{
			_dest.lock();
			_dest.fillRect(_rect, 0);
			/*
//			var node:Item = _st;
			
			while(node)
			{
				
//				_dest.copyPixels(sourceBitmapData, sourceRect, destPoint)
				
				node = node.next;
			}*
			*/
			_dest.unlock();
		}
		
		private function _test():void
		{
			var node:Item = _startNode;
			
			_dest.lock();
			_dest.fillRect(_rect, 0);
			
			const ww:int = _stripeWidth+_shiftX;
			const hh:int = _src.height;
			
			var rect:Rectangle = new Rectangle(0,0, ww, hh);
			var p:Point = new Point();
			
			while(node = node.next)
			{
				p.x = node.position.x;
				p.y = node.position.y;
				_dest.copyPixels(node.bmpd, rect, p, null,null, true);
			}
			
			_display.bitmapData = _dest;
		}
		
		private function _createMask():Shape
		{
			const h:int = _src.height;
			
			var s:Shape = new Shape();
			var g:Graphics = s.graphics;
			g.beginFill(0xFFFFFF);
			g.moveTo(0, h);
			g.lineTo(_stripeWidth+1, h);
			g.lineTo(_shiftX+_stripeWidth+1, 0);
			g.lineTo(_shiftX, 0);
			g.lineTo(0, h);
			
			return s;
		}
		
		
		
		/* CONTENT */
		private var _src:BitmapData;
		private var _dest:BitmapData;
		private var _display:Bitmap;
		private var _debugTest:Shape;
		
		/* VAR */
		private var _startNode:Item;
		
		private var _rect:Rectangle;
		private var _shiftX:int = 20;
		private var _shiftY:int = 20;
		private var _stripeWidth:int = 20;
		private var _progress : Number = 0;
		
		private var _coeffDir:Number;
	}
}


import flash.display.BitmapData;
import flash.geom.Point;

class Item
{
	public var position:Point = new Point();
	public var bmpd:BitmapData;
	public var next:Item;
	
	public function toString() : String
	{
		return "[SliceBmpItem : bmpd : '" + bmpd + "', position : '" + position +"']";
	}
	
}